package com.msb.serviceDriverUser.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.msb.internalcommon.constant.CommonStatusEnum;
import com.msb.internalcommon.constant.DriverCarConstants;
import com.msb.internalcommon.dto.DriverCarBindingRelationship;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.serviceDriverUser.mapper.DriverCarBindingRelationshipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author jiudianliu
 * @since 2023-07-03
 */
@Service
public class DriverCarBindingRelationshipService {

    @Autowired
    DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper ;


    public ResponseResult bind(DriverCarBindingRelationship driverCarBindingRelationship) {
        //判断  如果参数中司机跟车辆已经做过绑定，那么就不允许再绑定
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>() ;
        queryWrapper.eq("driver_id",driverCarBindingRelationship.getDriverId()) ;
        queryWrapper.eq("car_id",driverCarBindingRelationship.getCarId()) ;
        queryWrapper.eq("bind_state",DriverCarConstants.DRIVER_CAR_BIND) ;
        Integer integer = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if (integer.intValue() > 0) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getValue());
        }

        //判断  司机被绑定
        queryWrapper = new QueryWrapper<>() ;
        queryWrapper.eq("driver_id",driverCarBindingRelationship.getDriverId()) ;
        queryWrapper.eq("bind_state",DriverCarConstants.DRIVER_CAR_BIND) ;
        integer = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if (integer.intValue() > 0) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_BIND_EXISTS.getCode(),CommonStatusEnum.DRIVER_BIND_EXISTS.getValue());
        }

        //判断  车辆被绑定
        queryWrapper = new QueryWrapper<>() ;
        queryWrapper.eq("car_id",driverCarBindingRelationship.getCarId()) ;
        queryWrapper.eq("bind_state",DriverCarConstants.DRIVER_CAR_BIND) ;
        integer = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if (integer.intValue() > 0) {
            return ResponseResult.fail(CommonStatusEnum.CAR_BIND_EXISTS.getCode(),CommonStatusEnum.CAR_BIND_EXISTS.getValue());
        }

        LocalDateTime now = LocalDateTime.now() ;
        driverCarBindingRelationship.setBindingTime(now);
        driverCarBindingRelationship.setBindState(DriverCarConstants.DRIVER_CAR_BIND);
        driverCarBindingRelationshipMapper.insert(driverCarBindingRelationship);
        return ResponseResult.success(" ");
    }

    public ResponseResult unBind(DriverCarBindingRelationship driverCarBindingRelationship) {

        LocalDateTime now = LocalDateTime.now() ;

        Map<String, Object> map = new HashMap<>();
        map.put("driver_id",driverCarBindingRelationship.getDriverId()) ;
        map.put("car_id",driverCarBindingRelationship.getCarId()) ;
        map.put("bind_state",DriverCarConstants.DRIVER_CAR_BIND) ;

        List<DriverCarBindingRelationship> driverCarBindingRelationships = driverCarBindingRelationshipMapper.selectByMap(map);
        if (driverCarBindingRelationships.isEmpty()){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getValue()) ;
        }
        DriverCarBindingRelationship bindingRelationship = driverCarBindingRelationships.get(0);
        bindingRelationship.setBindState(DriverCarConstants.DRIVER_CAR_UNBIND);
        bindingRelationship.setUnBindingTime(now);
        driverCarBindingRelationshipMapper.updateById(bindingRelationship);
        return ResponseResult.success("");
    }
}
